﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Reflection;
using System.ComponentModel.Composition;
using System.IO;
using System.Data;
using System.Diagnostics;

namespace FTI.Testing
{
    /// <summary>
    /// Summary description for MillimanTest
    /// </summary>
    [TestClass]
    public class MillimanTest
    {
        public MillimanTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) 
        { 
           
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
            Composition oComposition = new Composition();

            oComposition.ComposeContainer(this);
        }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion
        [ImportMany(typeof(ISource))]
        IEnumerable<ISource> Sources { get; set; }

        [ImportMany(typeof(IDestination))]
        IEnumerable<IDestination> Destinations { get; set; }

        ISource Milliman
        {
            get
            {
                return Sources.SourceByName("Milliman Benefits");
            }
        }

        IDestination Quicken
        {
            get
            {
                return Destinations.DestinationByName("Quicken", Milliman);
            }
        }

        IDestination Money
        {
            get
            {
                return Destinations.DestinationByName("MS Money", Milliman);
            }
        }

        [TestMethod]
        public void VerifyMillimanDllIsLoadedFromExtensionFolder()
        {
            Assert.IsTrue(Milliman.Name.Eq("Milliman Benefits"), "Failed to load the Milliman Benefit source.");
            Assert.IsTrue(Quicken.Name.Eq("Quicken"), "Failed to load the Quicken Destination.");
            Assert.IsTrue(Money.Name.Eq("MS Money"), "Failed to load the MS Money Destination.");
        }

        [TestMethod]
        public void ProcessSourceFileUsingPrimaryThread()
        {
            DateTime
                Start,
                Finished;

            string path = "..\\..\\..\\Samples\\Milliman Sample.csv";

            Assert.IsTrue(File.Exists(path), String.Format("File does not exist."));

            DataSet oData = new DataSet();

            Start = DateTime.Now;

            Assert.IsTrue(Milliman.GetDataFromSource(path, ref oData), "Failed to grab data from source.");

            Finished = DateTime.Now;

            Trace.WriteLine(String.Format("Elapsed Time: {0}\r\n", (Finished - Start).ToString()));

            Trace.WriteLine(String.Format("Record Count: {0}\r\n", oData.Tables[0].Rows.Count));

            foreach(DataRow oDataRow in oData.Tables[0].Rows)
            {
                StringBuilder oOutPut = new StringBuilder();

                foreach(DataColumn oColumn in oData.Tables[0].Columns)
                {
                    oOutPut.AppendFormat("{0}|", oDataRow[oColumn.ColumnName]);
                }

                oOutPut
                    .Remove(oOutPut.Length - 1, 1)
                    .AppendLine();

                Trace.Write(oOutPut.ToString());
            }
        }

        //[TestMethod]
        public void ProcessSourceFileUsingMultiThreading()
        {
            DateTime
                Start,
                Finished;

            string path = "..\\..\\..\\Samples\\Milliman Sample.csv";

            Assert.IsTrue(File.Exists(path), String.Format("File does not exist."));

            DataSet oData = new DataSet();

            Milliman.EnableConcurrency = true;

            Start = DateTime.Now;

            Assert.IsTrue(Milliman.GetDataFromSource(path, ref oData), "Failed to grab data from source.");

            Finished = DateTime.Now;

            Trace.WriteLine(String.Format("Elapsed Time: {0}\r\n", (Finished - Start).ToString()));

            Trace.WriteLine(String.Format("Record Count: {0}\r\n", oData.Tables[0].Rows.Count));

            foreach (DataRow oDataRow in oData.Tables[0].Rows)
            {
                StringBuilder oOutPut = new StringBuilder();

                foreach (DataColumn oColumn in oData.Tables[0].Columns)
                {
                    oOutPut.AppendFormat("{0}|", oDataRow[oColumn.ColumnName]);
                }

                oOutPut
                    .Remove(oOutPut.Length - 1, 1)
                    .AppendLine();

                Trace.Write(oOutPut.ToString());
            }
        }
    }
}
